//
//  HJCNewFeatureCollectionViewController.swift
//  新浪微博Swift
//
//  Created by 郝俊聪 on 15/5/13.
//  Copyright (c) 2015年 heima. All rights reserved.
//

import UIKit



class HJCNewFeatureCollectionViewController: UICollectionViewController {
    
    @IBOutlet weak var layout: UICollectionViewFlowLayout!
    
   let imageCount = 4


    override func viewDidLoad() {
        super.viewDidLoad()
        //设置滚动属性
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
    

    }
    
    //即将显示的时候设置cell布局
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        layout.itemSize = self.view.bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
    }
    


    
}

extension HJCNewFeatureCollectionViewController
{
    
    
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return imageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("newfeature", forIndexPath: indexPath) as! HJCCollectionViewCell

        
        cell.imageIndex = indexPath.item + 1
        
        return cell
    }
    
    //结束展示cell
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //获得当前显示的的item的indexpath数组
        let indexPArray = collectionView.indexPathsForVisibleItems() as! [NSIndexPath]
        
        //取出indexpath
        let indexP = indexPArray.last!
        //当是最后一页时
        if indexP.item == imageCount - 1
        {
            //获得当前cell
            let cell = collectionView.cellForItemAtIndexPath(indexP) as! HJCCollectionViewCell
            
            //设置初始形变
            cell.beginBtn.transform = CGAffineTransformMakeScale(0, 0)
            
            //设置为显示
            cell.beginBtn.hidden = false
            
            
            //动画设置按钮显示
            UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options: nil, animations: { () -> Void in
                
                cell.beginBtn.transform = CGAffineTransformMakeScale(1, 1)
                
            }, completion: { (_) -> Void in
                
            })
        }
        
    }

    
}

class HJCCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var featureImageView: UIImageView!
 
    @IBOutlet weak var beginBtn: UIButton!
    
    
    @IBAction func clickBeginBtn() {
        
        //通知切换到主界面
        NSNotificationCenter.defaultCenter().postNotificationName(NSSWICTHCONTRONLLERNOTIFICATION, object: "Main")
        
    }
    

    //根据imageindex设置图片
    var imageIndex : Int = 1 {
        
        didSet{
            
            //设置按钮隐藏
            beginBtn.hidden = true
            
            //设置图片
            featureImageView.image = UIImage(named: "new_feature_\(imageIndex)")
        }
        
    }
    
    
}


